package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import objects.OrderDetail;

/**
 * Created by Monkey on 2017/6/19.
 * 医嘱计划复核适配器
 */

public class OrdersCheckAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private Map<OrderDetail, List<OrderDetail>> dataSets;
    private List<OrderDetail> parentsList;
    private List<Boolean> isChoicedList;
    private boolean isSeleceAll = false;

    public OrdersCheckAdapter(Context context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setDataSets(Map<OrderDetail, List<OrderDetail>> dataSets) {
        this.dataSets = dataSets;
        parentsList = new ArrayList<>();
        isChoicedList = new ArrayList<>();
        getParentList();
        Collections.sort(parentsList, new Comparator<OrderDetail>() {
            public int compare(OrderDetail arg0, OrderDetail arg1) {
                return arg0.getJHSJ().compareTo(arg1.getJHSJ());
            }
        });
    }

    private void getParentList() {
        Set set = dataSets.keySet();
        for (Object aSet : set) {
            parentsList.add((OrderDetail) aSet);
            isChoicedList.add(false);
        }
    }

    public boolean isSeleceAll() {
        return isSeleceAll;
    }

    public boolean isParentChoiced(int groupPosition) {
        if (isChoicedList != null && isChoicedList.get(groupPosition) != null) {
            return isChoicedList.get(groupPosition);
        }
        return false;
    }

    public OrderDetail getParent(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null) {
            return parentsList.get(groupPosition);
        }
        return null;
    }

    @Override
    public int getGroupCount() {
        if (parentsList != null && parentsList.size() > 0) {
            return parentsList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition)).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null) {
            return parentsList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (parentsList != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition)).get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_oc_parent, parent, false);
            holder.oc_cb = (CheckBox) convertView.findViewById(R.id.oc_checkbox);
            holder.oc_yzmc = (TextView) convertView.findViewById(R.id.oc_yzmc_text);
            holder.oc_yzlb = (TextView) convertView.findViewById(R.id.oc_yzlb_text);
            holder.oc_jhsj = (TextView) convertView.findViewById(R.id.oc_jhsj_text);
            holder.oc_ypyf = (TextView) convertView.findViewById(R.id.oc_ypyf_text);
            holder.oc_sypc = (TextView) convertView.findViewById(R.id.oc_sypc_text);
            holder.oc_ycjl = (TextView) convertView.findViewById(R.id.oc_ycjl_text);
            holder.oc_ycsl = (TextView) convertView.findViewById(R.id.oc_ycsl_text);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        if (groupPosition % 2 == 0) {
            convertView.setBackgroundResource(R.drawable.lv_even_line);
        } else {
            convertView.setBackgroundResource(R.drawable.lv_odd_line);
        }
        holder.oc_cb.setChecked(isChoicedList.get(groupPosition));
        holder.oc_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isChoicedList.set(groupPosition, !isChoicedList.get(groupPosition));
            }
        });
        if (parentsList != null && parentsList.size() > 0) {
            OrderDetail parentYZJH = parentsList.get(groupPosition);
            String yzmc = parentYZJH.getYZMC() == null ? "" : parentYZJH.getYZMC();
            holder.oc_yzmc.setText(yzmc);
            String yzlb = "";

            switch (parentYZJH.getLXXH()) {
                case "1":
                    yzlb = "输液";
                    break;
                case "2":
                    yzlb = "注射";
                    break;
                case "3":
                    yzlb = "口服";
                    break;
                case "4":
                    yzlb = "其他";
                    break;
            }
            holder.oc_yzlb.setText(yzlb);
            String jhsj = parentYZJH.getJHSJ() == null ? "" : parentYZJH.getJHSJ().substring(5, 16);
            holder.oc_jhsj.setText(jhsj);
            String ypyf = parentYZJH.getYFMC() == null ? "" : parentYZJH.getYFMC();
            holder.oc_ypyf.setText(ypyf);
            String sypc = parentYZJH.getSYPC() == null ? "" : parentYZJH.getSYPC();
            holder.oc_sypc.setText(sypc);
            String ycsl = parentYZJH.getYCSL() + " " + parentYZJH.getSLDW();
            holder.oc_ycsl.setText(ycsl);
            String ycjl = parentYZJH.getYCJL() + " " + parentYZJH.getJLDW();
            holder.oc_ycjl.setText(ycjl);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_oc_child, parent, false);
            holder.oc_yzmc = (TextView) convertView.findViewById(R.id.oc_yzmc_text);
            holder.oc_ycjl = (TextView) convertView.findViewById(R.id.oc_ycjl_text);
            holder.oc_ycsl = (TextView) convertView.findViewById(R.id.oc_ycsl_text);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        if (groupPosition % 2 == 0) {
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.even_bg));
        } else {
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.odd_bg));
        }
        List<OrderDetail> childList = dataSets.get(parentsList.get(groupPosition));
        if (childList != null && childList.size() > 0) {
            OrderDetail childYZJH = childList.get(childPosition);
            String yzmc = childYZJH.getYZMC() == null ? "" : childYZJH.getYZMC();
            holder.oc_yzmc.setText(yzmc);
            String ycsl = childYZJH.getYCSL() + " " + childYZJH.getSLDW();
            holder.oc_ycsl.setText(ycsl);
            String ycjl = childYZJH.getYCJL() + " " + childYZJH.getJLDW();
            holder.oc_ycjl.setText(ycjl);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ParentViewHolder {
        CheckBox oc_cb;
        TextView oc_yzmc;
        TextView oc_yzlb;
        TextView oc_jhsj;
        TextView oc_ypyf;
        TextView oc_sypc;
        TextView oc_ycjl;
        TextView oc_ycsl;
    }

    private class ChildViewHolder {
        TextView oc_yzmc;
        TextView oc_ycjl;
        TextView oc_ycsl;
    }

    //全选、全不选
    public void selectAll(boolean isSelectAll) {
        if (isChoicedList != null && isChoicedList.size() > 0) {
            for (int i = 0; i < isChoicedList.size(); i++) {
                isChoicedList.set(i, isSelectAll);
            }
        }
        this.isSeleceAll = isSelectAll;
        this.notifyDataSetChanged();
    }
}
